package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.cndragon.R;
import com.sskj.cndragon.presenter.ForgetPwdActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.base.BaseActivity;

import butterknife.BindView;


public class ForgetPwdActivity extends BaseActivity<ForgetPwdActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.rl_see)
    CheckBox rlSee;
    @BindView(R.id.rl_see_confirm)
    CheckBox rlSeeConfirm;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_mobile_code)
    EditText etMobileCode;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_pwd_again)
    EditText etPwdAgain;
    @BindView(R.id.btn_find)
    Button btnFind;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    public ForgetPwdActivityPresenter getPresenter() {
        return new ForgetPwdActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        toolbar.setLeftButtonOnClickListener(View -> finish());
        rlSee.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b) {
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        });
        rlSeeConfirm.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b) {
                etPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else
                etPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        });
        ClickUtil.click(300, tvGetCode, () -> {
            if (isEmpty(etMobile)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurunindeshoujihao));
                return;
            }
            if (!CheckUtil.isMobile(getEtext(etMobile))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruzhengquedeshoujihao));
                return;
            }
            mPresenter.getCode(getEtext(etMobile), BaseHttpConfig.CODE_RESET);
        });
        ClickUtil.click(300, btnFind, () -> {
            if (isEmpty(etMobile)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurunindeshoujihao));
                return;
            }
            if (!CheckUtil.isMobile(getEtext(etMobile))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruzhengquedeshoujihao));
                return;
            }
            if (isEmpty(etMobileCode)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruduanxinyanzhengma));
                return;
            }
            if (isEmpty(etPwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurudenglumima));
                return;
            }
            if (!CheckUtil.isPayPwd(getEtext(etPwd))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruzhengquedemima));
                return;
            }
            if (!getEtext(etPwd).equals(getEtext(etPwdAgain))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingmimabuyizhi));
                return;
            }
            mPresenter.findPwd(getEtext(etMobile), getEtext(etMobileCode), getEtext(etPwd));
        });
    }

    public void getCodeSuccess() {
        startTimeDown(tvGetCode);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ForgetPwdActivity.class);
        context.startActivity(intent);
    }

    public void resetSuccess() {
        finish();
    }
}
